package com.kele.goods.service;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.kele.common.model.response.PageResult;
import com.kele.common.model.response.Result;
import com.kele.goods.mapper.SpecificationMapper;
import com.kele.goods.mapper.SpecificationMapper1;
import com.kele.goods.mapper.SpecificationOptionMapper;
import com.kele.model.goods.Specification;
import com.kele.model.goods.SpecificationOption;
import com.kele.model.goods.dto.SpecificationDTO;
import com.kele.model.goods.dto.SpecificationDTO1;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Map;

@Service
public class SpecificationService {

    @Autowired
    SpecificationMapper specificationMapper;
    @Autowired
    SpecificationOptionMapper specificationOptionMapper;

    public List<Specification> findAll() {
        QueryWrapper<Specification> queryWrapper = new QueryWrapper<>();
        return specificationMapper.selectList(queryWrapper);
    }

    public PageResult<Specification> findPage(int pageNo, int pageSize, Map searchMap) {
        QueryWrapper<Specification> queryWrapper = new QueryWrapper<>();

        String specName = (String)searchMap.get("specName");
        if(!StringUtils.isEmpty(specName)){
            queryWrapper.lambda().like(Specification::getSpecName,specName);
        }
        Page<Specification> page = new Page<>(pageNo, pageSize);
        Page<Specification> specificationPage = specificationMapper.selectPage(page, queryWrapper);

        long total = specificationPage.getTotal();
        List<Specification> records = specificationPage.getRecords();
        return new PageResult<>(total,records);
    }

    public SpecificationDTO findById(Long id) {

        //1.先查主表
        Specification specification = specificationMapper.selectById(id);
        //2.再查子表
        QueryWrapper<SpecificationOption> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(SpecificationOption::getSpecId,id);
        List<SpecificationOption> specificationOptions = specificationOptionMapper.selectList(queryWrapper);

        //3创建返回对象
        SpecificationDTO specificationDTO = new SpecificationDTO();
        specificationDTO.setId(specification.getId());
        specificationDTO.setSpecName(specification.getSpecName());
        specificationDTO.setSpecificationOptions(specificationOptions);
        return specificationDTO;
    }

    @Transactional  //ACID  atomic consitency independency durable
    public Result add(SpecificationDTO specificationDTO) {
        //插入主表
        specificationMapper.insert(specificationDTO);
        Long specId = specificationDTO.getId();
        //获得ID
        List<SpecificationOption> specificationOptions = specificationDTO.getSpecificationOptions();

        //遍历所有的选项
        for (SpecificationOption specificationOption : specificationOptions) {
            specificationOption.setSpecId(specId);//设置规格ID
            specificationOptionMapper.insert(specificationOption);
        }
        return new Result(true,"1","插入规格成功");
    }
    @Transactional
    public Result update(SpecificationDTO specificationDTO) {
        //插入主表
        specificationMapper.updateById(specificationDTO);
        Long specId = specificationDTO.getId();
        //获得ID
        List<SpecificationOption> specificationOptions = specificationDTO.getSpecificationOptions();

        //先删掉以前的关系,条件删除
        UpdateWrapper<SpecificationOption> updateWrapper = new UpdateWrapper<>();
        updateWrapper.lambda().eq(SpecificationOption::getSpecId,specId);
        specificationOptionMapper.delete(updateWrapper);
        //遍历所有的选项
        for (SpecificationOption specificationOption : specificationOptions) {
            specificationOption.setSpecId(specId);//设置规格ID
            specificationOptionMapper.insert(specificationOption);
        }
        return new Result(true,"1","修改规格成功");
    }
    @Transactional
    public Result delete(Long id) {
        specificationMapper.deleteById(id);
        UpdateWrapper<SpecificationOption> updateWrapper = new UpdateWrapper<>();
        updateWrapper.lambda().eq(SpecificationOption::getSpecId,id);
        specificationOptionMapper.delete(updateWrapper);
        return new Result(true,"1","删除规格成功");
    }
    @Transactional
    public Result deleteByIds(Long[] ids) {
        for (Long id : ids) {
            delete(id);
        }
        return new Result(true,"1","批量删除规格成功");
    }


    @Autowired
    SpecificationMapper1 specificationMapper1;

    public PageResult<SpecificationDTO1> findPage1(int pageNo, int pageSize, Map searchMap) {

        QueryWrapper<SpecificationDTO1> queryWrapper = new QueryWrapper<>();

        String specName = (String)searchMap.get("specName");
        if(!StringUtils.isEmpty(specName)){
            queryWrapper.lambda().like(SpecificationDTO1::getSpecName,specName);
        }
        Page<SpecificationDTO1> page = new Page<>(pageNo, pageSize);
        //1 次查询
        Page<SpecificationDTO1> specificationPage = specificationMapper1.selectPage(page, queryWrapper);

        long total = specificationPage.getTotal();
        List<SpecificationDTO1> records = specificationPage.getRecords();

        //N 次查询
        records.forEach(record->{
            QueryWrapper<SpecificationOption> queryWrapper1 = new QueryWrapper<>();
            queryWrapper1.lambda().eq(SpecificationOption::getSpecId,record.getId());
            List<SpecificationOption> specificationOptions = specificationOptionMapper.selectList(queryWrapper1);
            record.setSpecificationOptions(specificationOptions);
        });
        return new PageResult<>(total,records);
    }

    public PageResult<SpecificationDTO1> findPage2(int pageNo, int pageSize, Map searchMap) {

        PageHelper.startPage(pageNo,pageSize);

        List<SpecificationDTO1> specificationDTO1s = specificationMapper1.selectSpecification();
        PageInfo<SpecificationDTO1> pageInfo = new PageInfo<>(specificationDTO1s);

        return new PageResult<>(pageInfo.getTotal(),pageInfo.getList());
    }
}
